package com.etongwl.mylibrary;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.etongwl.androiddownmodule.DownLoadController;
import com.etongwl.androiddownmodule.DownLoadControllerListener;
import com.etongwl.androiddownmodule.L;
import com.etongwl.androiddownmodule.Utils;
import com.etongwl.androiddownmodule.download.DownloadException;
import com.etongwl.androiddownmodule.download.DownloadRequest;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DownloadDemoActivity extends AppCompatActivity {

    @BindView(R.id.activity_download_demo_edittext)
    EditText edittext;
    @BindView(R.id.activity_download_demo_txt)
    TextView txt;
    @BindView(R.id.activity_download_demo_xz_btn)
    Button downloadBtn;
    @BindView(R.id.activity_download_demo_zt_btn)
    Button parseBtn;
    @BindView(R.id.activity_download_demo_name_edittext)
    EditText fileNameEdittext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_demo);
        ButterKnife.bind(this);

        //下载
        downloadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = null;
                if (!TextUtils.isEmpty(edittext.getText().toString())) {
                    url = edittext.getText().toString();
                } else {
                    url = "http://s1.music.126.net/download/android/CloudMusic_2.8.1_official_4.apk";
                }

                String fileName = null;
                if (!TextUtils.isEmpty(fileNameEdittext.getText().toString())){
                    fileName = fileNameEdittext.getText().toString();
                }else{
                    fileName = url.substring(url.lastIndexOf("/"));
                }
                DownloadRequest request = DownLoadController.getInstance(DownloadDemoActivity.this).downloadRequest(fileName, url, Utils.getDefaultDownloadDir(DownloadDemoActivity.this));
                DownLoadController.getInstance(DownloadDemoActivity.this).download(request, url, new DownLoadControllerListener() {
                    @Override
                    public void onCompleted() {

                        L.e(DownloadDemoActivity.class.getName(), "onCompleted");
                    }

                    @Override
                    public void onCompleted(String filePath) {
                        super.onCompleted(filePath);
                        L.e(DownloadDemoActivity.class.getName(), "onCompleted finepath=" + filePath);
                    }

                    @Override
                    public void onCompleted(File file) {
                        super.onCompleted(file);
                        L.e(DownloadDemoActivity.class.getName(), "onCompleted file=" + file.getAbsolutePath());
                    }

                    @Override
                    public void onConnected(long total, boolean isRangeSupport) {
                        super.onConnected(total, isRangeSupport);
                        L.e(DownloadDemoActivity.class.getName(), "onConnected total=" + total + ",isRangeSupport=" + isRangeSupport);
                    }

                    @Override
                    public void onConnecting() {
                        super.onConnecting();
                        L.e(DownloadDemoActivity.class.getName(), "onConnecting");

                    }

                    @Override
                    public void onDownloadCanceled() {
                        super.onDownloadCanceled();
                        L.e(DownloadDemoActivity.class.getName(), "onDownloadCanceled");
                    }

                    @Override
                    public void onDownloadPaused() {
                        super.onDownloadPaused();
                        L.e(DownloadDemoActivity.class.getName(), "onDownloadPaused");
                    }

                    @Override
                    public void onFailed(DownloadException e) {
                        super.onFailed(e);
                        e.printStackTrace();
                        L.e(DownloadDemoActivity.class.getName(), "onFailed");
                    }

                    @Override
                    public void onProgress(long finished, long total, int progress) {
                        super.onProgress(finished, total, progress);
                        L.e(DownloadDemoActivity.class.getName(), "onProgress finished=" + finished + ",total=" + total + ",progress=" + progress);
                    }

                    @Override
                    public void onStarted() {
                        super.onStarted();
                        L.e(DownloadDemoActivity.class.getName(), "onStarted");
                    }
                });
            }
        });
        //暂停
        parseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}
